﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace mForum.Infrastructure.NHibernate.Repository
{
    using mForum.Core.Common;
    using mForum.Domain.DomainObject;
    using mForum.Domain.Repository;
    using mForum.Domain.Specification;

    public class UserRepository : BaseRepository<User>, IUserRepository
    {
        public UserRepository(IDatabaseFactory dbFactory)
            : base(dbFactory)
        {

        }

        public int CountAllUsers()
        {
            return DatabaseContext.Users.Count();
        }

        public int CountNumberOfOnlineUsers()
        {
            UserOnlineUsersSpecification specification = new UserOnlineUsersSpecification();

            return this.GetBySpec(specification).Count<User>();
        }

        public IEnumerable<User> GetAllUsersOnline()
        {
            UserOnlineUsersSpecification specification = new UserOnlineUsersSpecification();

            return this.GetBySpec(specification);
        }

        public User FindUserByUsername(string username)
        {
            Check.Argument.IsNotEmpty(username, "username");

            UserUsernameSpecification specification = new UserUsernameSpecification(username);

            return this.GetBySpec(specification).SingleOrDefault<User>();
        }


        public User FindUserByEmail(string email)
        {
            Check.Argument.IsNotEmpty(email, "email");
            Check.Argument.IsNotInvalidEmail(email, "email");

            UserEmailSpecification specification = new UserEmailSpecification(email);

            return this.GetBySpec(specification).SingleOrDefault<User>();
        }
    }
}
